package topcbl.adapter;

import java.util.ArrayList;

import topcbl.data.HistoryPlace;
import topcbl.taxi.CallTaxiDialog;
import topcbl.taxi.MainActivity;
import topcbl.taxi.R;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HistoryAdapter extends ArrayAdapter<HistoryPlace> {
	private final Activity context;
	ArrayList<HistoryPlace> data;

	public HistoryAdapter(Activity context, ArrayList<HistoryPlace> arr) {
		super(context, R.layout.row_history_item, arr);
		this.context = context;
		this.data = arr;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		LayoutInflater inflater = context.getLayoutInflater();
		View rowView = inflater.inflate(R.layout.row_history_item, null, true);
		TextView txtAddress = (TextView) rowView
				.findViewById(R.id.tvTel);
		TextView txtTimeUsed = (TextView) rowView
				.findViewById(R.id.txtTimeUsed);
		ImageView btnDelete = (ImageView) rowView.findViewById(R.id.ImageView01);
		btnDelete.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				MainActivity.myDatabase.deleteHistory(data.get(position).getId());
				data.remove(position);
				notifyDataSetChanged();
			}
		});
		txtAddress.setText(data.get(position).getAddress());
		txtTimeUsed.setText(context.getString(R.string.history_time)
				+ ": " + data.get(position).getTime());
		rowView.setOnClickListener(new OnItemClickListener(position));
		return rowView;
	}

	private class OnItemClickListener implements OnClickListener {
		private int mPosition;

		OnItemClickListener(int position) {
			mPosition = position;
		}

		@Override
		public void onClick(View arg0) {
			new CallTaxiDialog(context, (data.get(mPosition))).show();
		}
	}
}
